$(function () {
    //新增促销员
    $("[name='btn_save_new_salesman']").click(function () {
        var form = window.form_new_salesman;
        var name = form.name.value;
        var phone = form.phone.value;
        var departmentId = form.departmentId.value;
        var position = form.position.value;
        var remark = form.remark.value;
        var state = form.state.value;

        var salesman = {name: name, phone: phone, departmentId: departmentId, position: position, remark: remark, state: state};
        $.postJsonBody("/salesman", salesman, function (re) {
            // tipper.success("已保存，请刷新页面");
            // $("#md_new_salesman").modal("hide");
        	window.location.href=window.location.href; 
        	window.location.reload; 
        })
    });


    //编辑促销员
    $("[name='btn_save_edit_salesman']").click(function () {
        var form = window.form_edit_salesman;
        var id = form.id.value;
        var name = form.name.value;
        var phone = form.phone.value;
        var departmentId = form.departmentId.value;
        var position = form.position.value;
        var remark = form.remark.value;
        var state = form.state.value;

        var salesman = {id: id, name: name, phone: phone, departmentId: departmentId, position: position, remark: remark, state: state};
        $.putJsonBody("/salesman/" + id, salesman, function (re) {
            // tipper.success("已更新，请刷新页面");
            // $("#md_edit_salesman").modal("hide");
        	window.location.href=window.location.href; 
        	window.location.reload; 
        })
    });

    //删除促销员
    $("[name='btn_delete_salesman']").click(function () {
        var salesmanId = $(this).data("salesman-id");
        bootbox.bconfirm("确定要删除这个促销员吗？", function (re) {
            if (re) {
                $.delete("/salesman/" + salesmanId, function (re) {
                    if (re.data) {
                        // tipper.success("删除成功，请刷新页面");
                    	window.location.href=window.location.href; 
                    	window.location.reload; 
                    } else {
                        tipper.danger("删除失败，请重试");
                    }
                });
            }
        });
    });


    // 促销员详情
    $('#md_edit_salesman').on('hide.bs.modal', function (event) {
        var form = window.form_edit_salesman;
        form.reset();
    });
    $('#md_edit_salesman').on('show.bs.modal', function (event) {
        var _e = $(event.relatedTarget);
        var id = _e.data("salesman-id");
        var modal = $(this);
        var form = window.form_edit_salesman;
        $.get("/salesman/" + id, function (re) {
            var salesman = re.data;
            form.id.value = salesman.id;
            form.name.value = salesman.name;
            form.phone.value = salesman.phone;
            $(form.departmentId).val(salesman.departmentId);
            form.position.value = salesman.position;
            form.remark.value = salesman.remark;
            $(form.state).val(salesman.state);
        });
    });

    //同步促销员至企业号
    $("#btn_start_sync_user_to_qyh").click(function () {
        $.post("/weixinqyh/sync/salesman", {departmentId: 1}, function (re) {
            console.log(re);
        });
    });

    // 任务详情
    $('#md_sync_user_to_qyh').on('show.bs.modal', function (event) {
    });


    //加载异步数据
    $.get("/department/list", function (re) {
        var departmentTree = re.data;
        var form = window.form_new_product;
        var opts_html = "";
        opts_html = departmentOptionMaker(departmentTree, 0);

        $(window.form_new_salesman.departmentId).append(opts_html);
        $(window.form_edit_salesman.departmentId).append(opts_html);
    });
});


function departmentOptionMaker(departmentList, level) {
    var opts_html = "";
    for (var i in departmentList) {
        var department = departmentList[i];

        var prefix = "";
        for (i = 0; i < level; i++) {
            prefix += "&nbsp;&nbsp;&nbsp;&nbsp;";
        }
        prefix += prefix.length == 0 ? "" : "|-";
        opts_html += "<option value='" + department.id + "'>" + prefix + department.name + "</option>";
        if (department.children.length > 0) {
            opts_html += departmentOptionMaker(department.children, level + 1);
        }
    }
    return opts_html;
}


//数据导出
$("[name='btn_export']").click(function () {
    var keyword = $("[name='keyword']").val();
    var state = $("[name='state']").val();
   
    //有数据时才能导出
    if($("#resultTotal_hidden").val() != 0){
    	bootbox.bconfirm("确定是否导出？", function (re) {
            if (re) {
            	 var url="/salesman/export?keyword="+keyword+"&state="+state;
                 window.open(url);
            }
        });
    }else{
    	bootbox.balert("没有数据，无法导出!");
    }
});